package com.wxproject.demo.Service.FackCheckService;

import com.baidu.aip.face.AipFace;
import com.google.gson.Gson;
import com.wxproject.demo.Entity.Bean.FaceBean.FaceVedioBean;
import com.wxproject.demo.Utils.RedisUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author zqf
 * @date 2021/4/4 23:27
 * @description
 */
@Component
public class FaceH5Check {
    private static String appId;

    @Value("${FaceApi.appId}")
    public void setAppId(String appId) {
        FaceH5Check.appId = appId;
    }


    public static FaceVedioBean getCode(AipFace client) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("appid", appId);
        // 语音校验码接口
        String uuid = UUID.randomUUID().toString();
        String res = client.videoSessioncode(options).toString(2);
        FaceVedioBean faceUserBean = new Gson().fromJson(res,FaceVedioBean.class);
        faceUserBean.setUuid(uuid);
        RedisUtil.set(uuid,faceUserBean.getResult().getSession_id());
        return faceUserBean;
    }

    public static String uploadH5Video(AipFace client,String videoName,String sessionId) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        // 参数为本地路径
        String res = client.videoFaceliveness(sessionId, "/www/wwwroot/files/static/file/"+videoName, options).toString();
        return res;
    }

    public static void SearchFace(AipFace client,String imageToken) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("max_face_num", "1");
        options.put("match_threshold", "80");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "NORMAL");
        options.put("max_user_num", "1");

        String image = imageToken;
        String imageType = "FACE_TOKEN";
        String groupIdList = "user";

        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupIdList, options);
        System.out.println(res.toString(2));

    }

}
